package tmt.usercenter.web.domain;

import com.tmt.annotation.CustomOptionValueLoader;
import com.tmt.annotation.IFieldOption;
import com.tmt.datatable.model.FieldOption;
import com.tmt.helper.ReflectHelper;
import com.tmt.spring.validation.RequiredField;
import tmt.usercenter.web.vo.user.RegisterVo;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

/**
 * 被反射创建。
 */
public class RequiredInfoLoader extends CustomOptionValueLoader {

	@Override
	public boolean support(Class clazz, String propName) {
		return clazz == UserInfoConfig.class && Arrays.asList("requiredInfo", "disableUpdate", "uniqueFields").contains(propName);
	}

	@Override
	public void load(Object obj, String propName, List<IFieldOption> fieldOptions) {
		List<String> fieldNameList = (List<String>) ReflectHelper.getFieldValue(obj, propName);

		List<Field> fieldList = ReflectHelper.findAllFieldByAnnotation(RegisterVo.class, RequiredField.class, -1);
		fieldList.stream().filter(x -> fieldNameList.contains(x.getName())).forEach(x -> {
			RequiredField annotation = x.getAnnotation(RequiredField.class);
			if (fieldOptions.stream().noneMatch(y->y.getValue() == x.getName())) {
				FieldOption fieldOption = new FieldOption();
				fieldOption.setLabel(annotation.label());
				fieldOption.setValue(x.getName());
				fieldOptions.add(fieldOption);
			}
		});

	}
}
